package com.org.handler;

import com.org.exception.BaseException;
import com.org.exception.ServerException;
import com.org.result.Result;
import com.org.result.ResultStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler(BaseException.class)
    public Result exceptionHandler(BaseException ex){
        log.error("用户异常操作，异常信息：{}", ex.getMessage());
        return Result.failure(400,ex.getMessage());
    }

    /**
     * 捕获服务器异常
     * @param ex
     * @return
     */
    @ExceptionHandler(ServerException.class)
    public Result exceptionHandler(ServerException ex){
        log.error("服务器异常，异常信息：{}", ex.getMessage());
        return Result.failure(500,ex.getMessage());
    }

}
